package com.weilive.api.gate.json.handler;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.api.ServerErrors;
import com.weilive.api.gate.json.protocol.CSA_GetAnchorInfo;
import com.weilive.api.gate.json.protocol.CSQ_GetAnchorInfo;
import com.weilive.api.service.UserSystem;
import com.weilive.core.protocol.json.JsonRequest;
import com.weilive.core.protocol.json.JsonRequestHandler;
import com.weilive.core.protocol.json.JsonResponse;

/**
 * 主播信息操作接口
 * 
 * @author houzhiwei
 *
 */
@Service
public class CSQ_GetAnchorInfo_JsonHandler extends JsonRequestHandler {

	private static final int TYPE_GET_ALL = 0;// 全部
	private static final int TYPE_GET_BASE_INF = 4;// 基本信息
	private static final int TYPE_GET_VIDEO_LIST = 5;// 历史影片
	private static final int TYPE_TOP_GIFT_LIST = 6;// 星星排行

	@Autowired
	private UserSystem userSystem;

	public Class<?> getRequestType() {
		return CSQ_GetAnchorInfo.class;
	}

	public Class<?> getResponseType() {
		return CSA_GetAnchorInfo.class;
	}

	// 处理cmd命令
	private Set<Integer> handleCmd(String cmd) {
		Set<Integer> cmdlist = new HashSet<Integer>();
		if (cmd == null || cmd.trim().length() == 0) {
			return cmdlist;
		}
		String[] cmds = cmd.split(",");

		for (String v : cmds) {
			int type = Integer.parseInt(v);
			// 全部
			if (type == 0) {
				cmdlist.clear();
				cmdlist.add(type);
				return cmdlist;
			}
			cmdlist.add(type);
		}
		return cmdlist;
	}

	public JsonResponse handleRequest(JsonRequest request) throws Exception {
		CSQ_GetAnchorInfo params = (CSQ_GetAnchorInfo) request.getData();

		if (StringUtils.isBlank(params.getCmdType()) || params.getAnchorUserId() == null) {
			return createErrorResponse(ServerErrors.PARAM_INVALID);
		}
		CSA_GetAnchorInfo response = new CSA_GetAnchorInfo();

		Set<Integer> cmd = handleCmd(params.getCmdType());

		int page = params.getPage() == null ? 0 : params.getPage();
		long userId = params.getClientUserId() == null ? 0L : params.getClientUserId();
		long anchorId = params.getAnchorUserId();

		response.setPage(page);

		for (Integer type : cmd) {
			if (type == TYPE_GET_ALL) {
				// 全部
				response.setAnchorInfo(userSystem.getAnchorInfoModel(userId, anchorId));
				response.setVideoList(userSystem.getVideoModelList(anchorId, page));
				response.setVideoShowList(userSystem.getVideoShowlList(anchorId));
				response.setRankList(userSystem.getGiftTop(anchorId, page));

			} else if (type == TYPE_GET_BASE_INF) {
				// 基本信息
				response.setAnchorInfo(userSystem.getAnchorInfoModel(userId, anchorId));
			} else if (type == TYPE_GET_VIDEO_LIST) {
				// 历史影片
				response.setVideoList(userSystem.getVideoModelList(anchorId, page));
				response.setVideoShowList(userSystem.getVideoShowlList(anchorId));
			} else if (type == TYPE_TOP_GIFT_LIST) {
				// 星星排行
				response.setRankList(userSystem.getGiftTop(anchorId, page));
			}
		}
		return JsonResponse.create(response);
	}

}
